 package cn.itcast.bos.service.system.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.bos.dao.system.MenuDao;
import cn.itcast.bos.domain.system.Menu;
import cn.itcast.bos.domain.system.User;
import cn.itcast.bos.service.system.MenuService;

@Service
@Transactional
public class MenuServiceImpl implements MenuService {
	// 注入Dao
	@Autowired
	private MenuDao md;

	/**
	 * 查询所有丹参
	 */
	@Override
	public List<Menu> findAll() {
		List<Menu> list = md.findAll();
		return list;
	}

	/**
	 * 保存菜单
	 */
	@Override
	public void save(Menu model) {
		// 防止用户没有选中父菜单报错
		if (model.getParentMenu() == null && model.getParentMenu().getId() == 0) {
			md.save(model);
		}
	}

	@Override
	public List<Menu> findByUser(User user) {
		if (user.getUsername().equals("admin")) {
			List<Menu> list = md.findAll();
			return list;
		} else {
			// 通过用户查找菜单
			List<Menu> findByUser = md.findByUser(user.getId());
			return findByUser;
		}
	}

}
